
# ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
# DESCRIPTION
# ______________________________________________________________________________

# This code creates Figures 5, 7, B.2, and D.2.

# Preamble ---------------------------------------------------------------------

rm(list = ls())

# Packages ---------------------------------------------------------------------

library(tidyverse)
library(ggrepel)
library(haven)
library(janitor)
library(paletteer)
library(patchwork)
library(scales)

# Main Analysis ----------------------------------------------------------------

## Data ------------------------------------------------------------------------

main_rebel_0123_pp <-
  read_dta("./results/main_rebel_pp.dta") %>%
  mutate(
    rebelsv = gsub("(?<!^).", "", parm, perl = TRUE),
    rebelsv = as.numeric(rebelsv) - 1,
    secessionistconflict = gsub("[^0-9]", "", parm),
    secessionistconflict = substring(secessionistconflict, 2),
    secessionistconflict = as.numeric(secessionistconflict) - 1
  ) %>%
  filter(rebelsv != 0)

main_rebel_inflated0_pp <-
  read_dta("./results/main_rebel_inflated0_pp.dta") %>%
  mutate(
    secessionistconflict = gsub("[^0-9]", "", parm),
    secessionistconflict = as.numeric(secessionistconflict) - 1,
    rebelsv = "-99"
  ) %>%
  relocate(rebelsv, .before = secessionistconflict)

main_rebel_true0_pp <-
  read_dta("./results/main_rebel_true0_pp.dta") %>%
  mutate(
    secessionistconflict = gsub("[^0-9]", "", parm),
    secessionistconflict = as.numeric(secessionistconflict) - 1,
    rebelsv = "0"
  ) %>%
  relocate(rebelsv, .before = secessionistconflict)

main_rebel_pp <-
  rbind(main_rebel_inflated0_pp,
        main_rebel_true0_pp,
        main_rebel_0123_pp) %>%
  mutate(rebelsv = as.character(rebelsv),
         rebelsv = factor(rebelsv, 
                          levels = c("-99", "0", "1", "2", "3"))) %>%
  mutate(legend1 = factor(rebelsv, 
                          labels = c("Inflated 0", "True 0", "1", "2", "3")))

main_state_0123_pp <-
  read_dta("./results/main_state_pp.dta") %>%
  mutate(
    statesv = gsub("(?<!^).", "", parm, perl = TRUE),
    statesv = as.numeric(statesv) - 1,
    secessionistconflict = gsub("[^0-9]", "", parm),
    secessionistconflict = substring(secessionistconflict, 2),
    secessionistconflict = as.numeric(secessionistconflict) - 1
  ) %>%
  filter(statesv != 0)

main_state_inflated0_pp <-
  read_dta("./results/main_state_inflated0_pp.dta") %>%
  mutate(
    secessionistconflict = gsub("[^0-9]", "", parm),
    secessionistconflict = as.numeric(secessionistconflict) - 1,
    statesv = "-99"
  ) %>%
  relocate(statesv, .before = secessionistconflict)

main_state_true0_pp <-
  read_dta("./results/main_state_true0_pp.dta") %>%
  mutate(
    secessionistconflict = gsub("[^0-9]", "", parm),
    secessionistconflict = as.numeric(secessionistconflict) - 1,
    statesv = "0"
  ) %>%
  relocate(statesv, .before = secessionistconflict)

main_state_pp <-
  rbind(main_state_inflated0_pp,
        main_state_true0_pp,
        main_state_0123_pp) %>%
  mutate(statesv = as.character(statesv),
         statesv = factor(statesv, 
                          levels = c("-99", "0", "1", "2", "3"))) %>%
  mutate(legend2 = factor(statesv, 
                          labels = c("Inflated 0", "True 0", "1", "2", "3")))

## Plot 1 ----------------------------------------------------------------------

a <- main_rebel_pp %>%
  ggplot(aes(
    x = secessionistconflict,
    y = estimate,
    color = legend1,
    group = legend1
  )) +
  geom_path(lineend = "round") +
  geom_errorbar(aes(
    ymin = ifelse(min90 < 0, -0.045, min90),
    ymax = ifelse(max90 > 0.9, 0.95, max90)
  ),
  width = 0.02,
  lineend = "round") +
  geom_point(
    data = subset(main_rebel_pp, legend1 == "Inflated 0"),
    aes(shape = legend1, fill = legend1),
    size = 2,
    color = "black"
  ) +
  geom_point(
    data = subset(main_rebel_pp, legend1 == "Inflated 0"),
    aes(shape = legend1, fill = legend1),
    size = 2,
    stroke = 1,
    color = "blue",
    shape = 4
  ) +
  geom_point(
    data = subset(main_rebel_pp, legend1 != "Inflated 0"),
    aes(shape = legend1, fill = legend1),
    size = 2,
    color = "black"
  ) +
  geom_text_repel(
    aes(label = sprintf("%.0f%%", estimate * 100)),
    size = 4,
    box.padding = 0.05,
    point.padding = 0.05,
    force = 1,
    direction = "y",
    nudge_x = ifelse(main_rebel_pp$secessionistconflict == 0, -0.06, 0.06),
    segment.color = NA,
    show.legend = FALSE
  ) +
  scale_x_continuous(breaks = c(0, 1), 
                     labels = c("No", "Yes")) +
  scale_y_continuous(breaks = seq(0, 0.9, 0.3), 
                     labels = rev(c("90%", "60%", "30%", "0%"))) +
  scale_shape_manual(values = c(
    "Inflated 0" = 21,
    "True 0" = 21,
    "1" = 24,
    "2" = 22,
    "3" = 23
  )) +
  scale_color_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  scale_fill_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  facet_wrap(. ~ "Rebel SV") +
  labs(
    x = "Secessionist Conflict",
    y = "Predicted Probability (%)",
    color = "Prevalence Level",
    fill = "Prevalence Level",
    shape = "Prevalence Level"
  ) +
  theme_classic() +
  theme(
    legend.background = element_blank(),
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.grid.major.y = element_line(),
    axis.text = element_text(color = "black"),
    axis.ticks = element_line(color = "black"),
    panel.border = element_rect(fill = NA, color = "black")
  ) +
  coord_cartesian(ylim = c(0, 0.9), clip = "off")

b <- main_state_pp %>%
  ggplot(aes(
    x = secessionistconflict,
    y = estimate,
    color = legend2,
    group = legend2
  )) +
  geom_path(lineend = "round") +
  geom_errorbar(aes(ymin = min90, ymax = max90),
                width = 0.02,
                lineend = "round") +
  geom_point(
    data = subset(main_state_pp, legend2 == "Inflated 0"),
    aes(shape = legend2, fill = legend2),
    size = 2,
    color = "black"
  ) +
  geom_point(
    data = subset(main_state_pp, legend2 == "Inflated 0"),
    aes(shape = legend2, fill = legend2),
    size = 2,
    stroke = 1,
    color = "blue",
    shape = 4
  ) +
  geom_point(
    data = subset(main_state_pp, legend2 != "Inflated 0"),
    aes(shape = legend2, fill = legend2),
    size = 2,
    color = "black"
  ) +
  geom_text_repel(
    aes(label = sprintf("%.0f%%", estimate * 100)),
    size = 4,
    box.padding = 0.05,
    point.padding = 0.05,
    force = 1,
    direction = "y",
    nudge_x = ifelse(main_state_pp$secessionistconflict == 0, -0.06, 0.06),
    segment.color = NA,
    show.legend = FALSE
  ) +
  scale_x_continuous(breaks = c(0, 1), 
                     labels = c("No", "Yes")) +
  scale_y_continuous(breaks = seq(0, 0.6, 0.2), 
                     labels = rev(c("60%", "40%", "20%", "0%"))) +
  scale_shape_manual(values = c(
    "Inflated 0" = 21,
    "True 0" = 21,
    "1" = 24,
    "2" = 22,
    "3" = 23
  )) +
  scale_color_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  scale_fill_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  facet_wrap(. ~ "State SV") +
  labs(
    x = "Secessionist Conflict",
    y = "Predicted Probability (%)",
    color = "Prevalence Level",
    fill = "Prevalence Level",
    shape = "Prevalence Level"
  ) +
  theme_classic() +
  theme(
    legend.background = element_blank(),
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.grid.major.y = element_line(),
    axis.title.y = element_blank(),
    axis.text = element_text(color = "black"),
    axis.ticks = element_line(color = "black"),
    panel.border = element_rect(fill = NA, color = "black")
  ) +
  coord_cartesian(ylim = c(0, 0.6), clip = "off")

a + b + plot_layout(guides = "collect") &
  theme(
    legend.position = "bottom",
    legend.box.margin = margin(-10, 0, -5, 0, "pt"),
    legend.spacing.y = unit(0, "pt"),
    legend.key.width = unit(30, "pt"),
    plot.margin = margin(3, 3, 0.5, 2.5, "pt"),
    strip.text = element_text(size = 12, margin = margin(t = 7.5, b = 7.5)),
    text = element_text(size = 12),
    axis.text = element_text(size = 12),
    legend.text = element_text(size = 12)
  )

ggsave(
  "figures/predicted probability plots/predicted_probability_plot_1.pdf",
  width = 10,
  height = 5
)

## Plot 2 ----------------------------------------------------------------------

a <- main_rebel_pp %>%
  ggplot(aes(
    x = secessionistconflict,
    y = estimate,
    color = legend1,
    group = legend1
  )) +
  geom_path(lineend = "round") +
  geom_errorbar(
    aes(ymin = min90, ymax = max90),
    width = 0.02,
    lineend = "round",
    alpha = 0
  ) +
  geom_point(
    data = subset(main_rebel_pp, legend1 == "Inflated 0"),
    aes(shape = legend1, fill = legend1),
    size = 2,
    color = "black"
  ) +
  geom_point(
    data = subset(main_rebel_pp, legend1 == "Inflated 0"),
    aes(shape = legend1, fill = legend1),
    size = 2,
    stroke = 1,
    color = "blue",
    shape = 4
  ) +
  geom_point(
    data = subset(main_rebel_pp, legend1 != "Inflated 0"),
    aes(shape = legend1, fill = legend1),
    size = 2,
    color = "black"
  ) +
  geom_text_repel(
    aes(label = sprintf("%.0f%%", estimate * 100)),
    size = 4,
    box.padding = 0.05,
    point.padding = 0.05,
    force = 1,
    direction = "y",
    nudge_x = ifelse(main_rebel_pp$secessionistconflict == 0, -0.06, 0.06),
    segment.color = NA,
    show.legend = FALSE
  ) +
  scale_x_continuous(breaks = c(0, 1), 
                     labels = c("No", "Yes")) +
  scale_y_continuous(breaks = seq(0, 0.9, 0.3), 
                     labels = rev(c("90%", "60%", "30%", "0%"))) +
  scale_shape_manual(values = c(
    "Inflated 0" = 21,
    "True 0" = 21,
    "1" = 24,
    "2" = 22,
    "3" = 23
  )) +
  scale_color_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  scale_fill_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  facet_wrap(. ~ "Rebel SV") +
  labs(
    x = "Secessionist Conflict",
    y = "Predicted Probability (%)",
    color = "Prevalence Level",
    fill = "Prevalence Level",
    shape = "Prevalence Level"
  ) +
  theme_classic() +
  theme(
    legend.background = element_blank(),
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.grid.major.y = element_line(),
    axis.text = element_text(color = "black"),
    axis.ticks = element_line(color = "black"),
    panel.border = element_rect(fill = NA, color = "black")
  ) +
  coord_cartesian(ylim = c(0, 0.9), clip = "off")

b <- main_state_pp %>%
  ggplot(aes(
    x = secessionistconflict,
    y = estimate,
    color = legend2,
    group = legend2
  )) +
  geom_path(lineend = "round") +
  geom_errorbar(
    aes(ymin = min90, ymax = max90),
    width = 0.02,
    lineend = "round",
    alpha = 0
  ) +
  geom_point(
    data = subset(main_state_pp, legend2 == "Inflated 0"),
    aes(shape = legend2, fill = legend2),
    size = 2,
    color = "black"
  ) +
  geom_point(
    data = subset(main_state_pp, legend2 == "Inflated 0"),
    aes(shape = legend2, fill = legend2),
    size = 2,
    stroke = 1,
    color = "blue",
    shape = 4
  ) +
  geom_point(
    data = subset(main_state_pp, legend2 != "Inflated 0"),
    aes(shape = legend2, fill = legend2),
    size = 2,
    color = "black"
  ) +
  geom_text_repel(
    aes(label = sprintf("%.0f%%", estimate * 100)),
    size = 4,
    box.padding = 0.05,
    point.padding = 0.05,
    force = 1,
    direction = "y",
    nudge_x = ifelse(main_state_pp$secessionistconflict == 0, -0.06, 0.06),
    segment.color = NA,
    show.legend = FALSE
  ) +
  scale_x_continuous(breaks = c(0, 1), 
                     labels = c("No", "Yes")) +
  scale_y_continuous(breaks = seq(0, 0.6, 0.2), 
                     labels = rev(c("60%", "40%", "20%", "0%"))) +
  scale_shape_manual(values = c(
    "Inflated 0" = 21,
    "True 0" = 21,
    "1" = 24,
    "2" = 22,
    "3" = 23
  )) +
  scale_color_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  scale_fill_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  facet_wrap(. ~ "State SV") +
  labs(
    x = "Secessionist Conflict",
    y = "Predicted Probability (%)",
    color = "Prevalence Level",
    fill = "Prevalence Level",
    shape = "Prevalence Level"
  ) +
  theme_classic() +
  theme(
    legend.background = element_blank(),
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.grid.major.y = element_line(),
    axis.title.y = element_blank(),
    axis.text = element_text(color = "black"),
    axis.ticks = element_line(color = "black"),
    panel.border = element_rect(fill = NA, color = "black")
  ) +
  coord_cartesian(ylim = c(0, 0.6), clip = "off")

a + b + plot_layout(guides = "collect") &
  theme(
    legend.position = "bottom",
    legend.box.margin = margin(-10, 0, -5, 0, "pt"),
    legend.spacing.y = unit(0, "pt"),
    legend.key.width = unit(30, "pt"),
    plot.margin = margin(3, 3, 0.5, 2.5, "pt"),
    strip.text = element_text(size = 12, margin = margin(t = 7.5, b = 7.5)),
    text = element_text(size = 12),
    axis.text = element_text(size = 12),
    legend.text = element_text(size = 12)
  )

ggsave(
  "figures/predicted probability plots/predicted_probability_plot_2.pdf",
  width = 10,
  height = 5
)

# Longitudinal Analysis --------------------------------------------------------

## Data ------------------------------------------------------------------------

longitudinal_rebel_0123_pp <-
  read_dta("./results/longitudinal_rebel_pp.dta") %>%
  mutate(
    rebelsv = gsub("(?<!^).", "", parm, perl = TRUE),
    rebelsv = as.numeric(rebelsv) - 1,
    duration = gsub("[^0-9]", "", parm),
    duration = substring(duration, 2),
    duration = as.numeric(duration) - 1,
  ) %>%
  filter(rebelsv != 0)

longitudinal_rebel_inflated0_pp <-
  read_dta("./results/longitudinal_rebel_inflated0_pp.dta") %>%
  mutate(
    duration = gsub("[^0-9]", "", parm),
    duration = as.numeric(duration) - 1,
    rebelsv = "-99"
  ) %>%
  relocate(rebelsv, .before = duration)

longitudinal_rebel_true0_pp <-
  read_dta("./results/longitudinal_rebel_true0_pp.dta") %>%
  mutate(
    duration = gsub("[^0-9]", "", parm),
    duration = as.numeric(duration) - 1,
    rebelsv = "0"
  ) %>%
  relocate(rebelsv, .before = duration)

longitudinal_rebel_pp <-
  rbind(
    longitudinal_rebel_inflated0_pp,
    longitudinal_rebel_true0_pp,
    longitudinal_rebel_0123_pp
  ) %>%
  mutate(rebelsv = as.character(rebelsv),
         rebelsv = factor(rebelsv, 
                          levels = c("-99", "0", "1", "2", "3"))) %>%
  mutate(legend1 = factor(rebelsv, 
                          labels = c("Inflated 0", "True 0", "1", "2", "3")))

longitudinal_state_0123_pp <-
  read_dta("./results/longitudinal_state_pp.dta") %>%
  mutate(
    statesv = gsub("(?<!^).", "", parm, perl = TRUE),
    statesv = as.numeric(statesv) - 1,
    duration = gsub("[^0-9]", "", parm),
    duration = substring(duration, 2),
    duration = as.numeric(duration) - 1,
  ) %>%
  filter(statesv != 0)

longitudinal_state_inflated0_pp <-
  read_dta("./results/longitudinal_state_inflated0_pp.dta") %>%
  mutate(
    duration = gsub("[^0-9]", "", parm),
    duration = as.numeric(duration) - 1,
    statesv = "-99"
  ) %>%
  relocate(statesv, .before = duration)

longitudinal_state_true0_pp <-
  read_dta("./results/longitudinal_state_true0_pp.dta") %>%
  mutate(
    duration = gsub("[^0-9]", "", parm),
    duration = as.numeric(duration) - 1,
    statesv = "0"
  ) %>%
  relocate(statesv, .before = duration)

longitudinal_state_pp <-
  rbind(
    longitudinal_state_inflated0_pp,
    longitudinal_state_true0_pp,
    longitudinal_state_0123_pp
  ) %>%
  mutate(statesv = as.character(statesv),
         statesv = factor(statesv, 
                          levels = c("-99", "0", "1", "2", "3"))) %>%
  mutate(legend2 = factor(statesv, 
                          labels = c("Inflated 0", "True 0", "1", "2", "3")))

## Plot 3 ----------------------------------------------------------------------

a <- longitudinal_rebel_pp %>%
  ggplot(aes(
    x = duration,
    y = estimate,
    color = legend1,
    fill = legend1
  )) +
  geom_path(lineend = "round") +
  geom_point(
    data = subset(longitudinal_rebel_pp, legend1 == "Inflated 0"),
    aes(shape = legend1, fill = legend1),
    size = 2,
    color = "black"
  ) +
  geom_point(
    data = subset(longitudinal_rebel_pp, legend1 == "Inflated 0"),
    aes(shape = legend1, fill = legend1),
    size = 2,
    stroke = 1,
    color = "blue",
    shape = 4
  ) +
  geom_point(
    data = subset(longitudinal_rebel_pp, legend1 != "Inflated 0"),
    aes(shape = legend1, fill = legend1),
    size = 2,
    color = "black"
  ) +
  scale_x_continuous(expand = expansion(mult = c(0.04, 0.04))) +
  scale_y_continuous(breaks = seq(0, 1, 0.5),
                     labels = c("0%", "50%", "100%")) +
  scale_shape_manual(values = c(
    "Inflated 0" = 21,
    "True 0" = 21,
    "1" = 24,
    "2" = 22,
    "3" = 23
  )) +
  scale_color_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  scale_fill_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  labs(
    x = "Secessionist Conflict Duration (Year)",
    y = "Predicted Probability (%)",
    color = "Prevalence Level",
    fill = "Prevalence Level",
    shape = "Prevalence Level"
  ) +
  facet_wrap(. ~ "Rebel SV") +
  theme_classic() +
  theme(
    panel.grid.major.y = element_line(),
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    legend.position = "bottom",
    legend.spacing.y = unit(0, "pt"),
    legend.key.width = unit(30, "pt"),
    legend.box.margin = margin(-10, 0, -5, 0, "pt"),
    strip.text = element_text(size = 12),
    axis.text = element_text(color = "black"),
    axis.ticks = element_line(color = "black"),
    panel.border = element_rect(fill = NA, color = "black")
  ) +
  coord_cartesian(xlim = c(0, 47),
                  ylim = c(0, 1),
                  clip = "off")

b <- longitudinal_state_pp %>%
  ggplot(aes(
    x = duration,
    y = estimate,
    color = legend2,
    fill = legend2
  )) +
  geom_path(lineend = "round") +
  geom_point(
    data = subset(longitudinal_state_pp, legend2 == "Inflated 0"),
    aes(shape = legend2, fill = legend2),
    size = 2,
    color = "black"
  ) +
  geom_point(
    data = subset(longitudinal_state_pp, legend2 == "Inflated 0"),
    aes(shape = legend2, fill = legend2),
    size = 2,
    stroke = 1,
    color = "blue",
    shape = 4
  ) +
  geom_point(
    data = subset(longitudinal_state_pp, legend2 != "Inflated 0"),
    aes(shape = legend2, fill = legend2),
    size = 2,
    color = "black"
  ) +
  scale_x_continuous(expand = expansion(mult = c(0.04, 0.04))) +
  scale_y_continuous(breaks = seq(0, 0.6, 0.3),
                     labels = c("0%", "30%", "60%")) +
  scale_shape_manual(values = c(
    "Inflated 0" = 21,
    "True 0" = 21,
    "1" = 24,
    "2" = 22,
    "3" = 23
  )) +
  scale_color_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  scale_fill_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  labs(
    x = "Secessionist Conflict Duration (Year)",
    y = "Predicted Probability (%)",
    color = "Prevalence Level",
    fill = "Prevalence Level",
    shape = "Prevalence Level"
  ) +
  facet_wrap(. ~ "State SV") +
  theme_classic() +
  theme(
    panel.grid.major.y = element_line(),
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    legend.position = "bottom",
    legend.spacing.y = unit(0, "pt"),
    legend.key.width = unit(30, "pt"),
    legend.box.margin = margin(-10, 0, -5, 0, "pt"),
    strip.text = element_text(size = 12),
    axis.title.y = element_blank(),
    axis.text = element_text(color = "black"),
    axis.ticks = element_line(color = "black"),
    panel.border = element_rect(fill = NA, color = "black")
  ) +
  coord_cartesian(xlim = c(0, 47),
                  ylim = c(0, 0.6),
                  clip = "off")

a + b + plot_layout(guides = "collect") &
  theme(
    legend.position = "bottom",
    legend.box.margin = margin(-10, 0, -5, 0, "pt"),
    legend.spacing.y = unit(0, "pt"),
    legend.key.width = unit(30, "pt"),
    plot.margin = margin(3, 3, 0.5, 2.5, "pt"),
    strip.text = element_text(size = 12, margin = margin(t = 7.5, b = 7.5)),
    text = element_text(size = 12),
    axis.text = element_text(size = 12),
    legend.text = element_text(size = 12)
  )

ggsave(
  "figures/predicted probability plots/predicted_probability_plot_3.pdf",
  width = 10,
  height = 5
)

## Plot 4 ----------------------------------------------------------------------

a <- longitudinal_rebel_pp %>%
  ggplot(aes(
    x = ifelse(rebelsv %in% c("1", "2", "3"), NA, duration),
    y = estimate,
    color = legend1,
    fill = legend1,
    shape = legend1
  )) +
  geom_path(lineend = "round") +
  geom_point(size = 2, color = "black") +
  geom_point(
    data = filter(longitudinal_rebel_pp, legend1 == "Inflated 0"),
    size = 2,
    color = "blue",
    shape = 4,
    stroke = 1
  ) +
  scale_x_continuous(expand = expansion(mult = c(0.04, 0.04))) +
  scale_y_continuous(breaks = c(0, 0.5, 1), labels = percent_format()) +
  scale_shape_manual(values = c(
    "Inflated 0" = 21,
    "True 0" = 21,
    "1" = 24,
    "2" = 22,
    "3" = 23
  )) +
  scale_color_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  scale_fill_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  labs(
    x = "Secessionist Conflict Duration (Year)",
    y = "Predicted Probability (%)",
    color = "Prevalence Level",
    fill = "Prevalence Level",
    shape = "Prevalence Level"
  ) +
  facet_wrap(. ~ "Rebel SV") +
  theme_classic() +
  theme(
    panel.grid.major.y = element_line(),
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    legend.position = "bottom",
    legend.spacing.y = unit(0, "pt"),
    legend.key.width = unit(30, "pt"),
    legend.box.margin = margin(-10, 0, -5, 0, "pt"),
    strip.text = element_text(size = 12),
    axis.text = element_text(color = "black"),
    axis.ticks = element_line(color = "black"),
    panel.border = element_rect(fill = NA, color = "black")
  ) +
  coord_cartesian(xlim = c(0, 47),
                  ylim = c(0, 1),
                  clip = "off")

b <- longitudinal_state_pp %>%
  ggplot(aes(
    x = ifelse(statesv %in% c(-99, 0), NA, duration),
    y = estimate,
    color = legend2,
    fill = legend2,
    shape = legend2
  )) +
  geom_path(lineend = "round") +
  geom_point(size = 2, color = "black") +
  geom_point(
    data = filter(longitudinal_state_pp, legend2 == "Inflated 0"),
    aes(
      x = 100,
      color = legend2,
      fill = legend2,
      shape = legend2
    ),
    size = 2,
    color = "blue",
    shape = 4,
    stroke = 1
  ) +
  scale_x_continuous(expand = expansion(mult = c(0.04, 0.04))) +
  scale_y_continuous(breaks = c(0, 0.25, 0.5), labels = percent_format()) +
  scale_shape_manual(values = c(
    "Inflated 0" = 21,
    "True 0" = 21,
    "1" = 24,
    "2" = 22,
    "3" = 23
  )) +
  scale_color_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  scale_fill_manual(
    values = c(
      "Inflated 0" = "blue",
      "True 0" = "thistle",
      "1" = "violet",
      "2" = "purple",
      "3" = "red"
    )
  ) +
  labs(
    x = "Secessionist Conflict Duration (Year)",
    y = "Predicted Probability (%)",
    color = "Prevalence Level",
    fill = "Prevalence Level",
    shape = "Prevalence Level"
  ) +
  facet_wrap(. ~ "State SV") +
  theme_classic() +
  theme(
    panel.grid.major.y = element_line(),
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    legend.position = "bottom",
    legend.spacing.y = unit(0, "pt"),
    legend.key.width = unit(30, "pt"),
    legend.box.margin = margin(-10, 0, -5, 0, "pt"),
    strip.text = element_text(size = 12),
    axis.title.y = element_blank(),
    axis.text = element_text(color = "black"),
    axis.ticks = element_line(color = "black"),
    panel.border = element_rect(fill = NA, color = "black")
  ) +
  coord_cartesian(xlim = c(0, 47),
                  ylim = c(0, 0.5),
                  clip = "off")

a + b + plot_layout(guides = "collect") &
  theme(
    legend.position = "bottom",
    legend.box.margin = margin(-10, 0, -5, 0, "pt"),
    legend.spacing.y = unit(0, "pt"),
    legend.key.width = unit(30, "pt"),
    plot.margin = margin(3, 3, 0.5, 2.5, "pt"),
    strip.text = element_text(size = 12, margin = margin(t = 7.5, b = 7.5)),
    text = element_text(size = 12),
    axis.text = element_text(size = 12),
    legend.text = element_text(size = 12)
  )

ggsave(
  "figures/predicted probability plots/predicted_probability_plot_4.pdf",
  width = 10,
  height = 5
)
